import { _decorator, Button, Label } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import { ClientAPI } from '../../api/api.client';
import GameDC from '../../data/data.game.center';
import DataManager from '../../manager/data.manager';
import { Float } from '../page/float';
import type { Popup } from '../page/pop-up';
const { ccclass, property } = _decorator;

@ccclass('ppResurgence')
export class ppResurgence extends BasePopUp<typeof Popup, boolean> {
	protected default_return: null;

	protected initAfterOnLoad(){
		this.init();
	}

	public onShow(){
		this.btn_lock = false;
		BasePopUp.jelly_enter(this.node);
	}

	private init(){
		(<const>[
			['lbl-head', 'Resurrection?'],
			['lbl-diamond', '100'],
			['lbl-video', 'CLAIM'],
		]).forEach(([name, text])=>this.node.getChildByName(name).getComponent(Label).string = text);

		this.node.getComponentsInChildren(Label).filter(lbl=>lbl.node.name=='close')[0].string = 'No Thanks';
	}

	private btn_lock = false;
	protected _click_event_: { [name: string]: (this: ppResurgence, button: Button) => void; } = {
		close(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			this.exit(false);
		},
		async diamond(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			let diamond = GameDC.User.diamond;
			if(!(diamond >= 100)){
				await this.pop.Diamond();
				diamond = GameDC.User.diamond;
			}

			if(diamond >= 100){
				DataManager.changeDiamond(-100);
				DataManager.floatDiamond(-100);
				this.exit(true);
			}else{
				Float.Hint('Withdraw all funds after passing！');
				this.btn_lock = false;
			}
		},
		video(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			ClientAPI.AD('incentive').then(success=>{
				if(success){
					this.exit(true);
				}else{
					this.btn_lock = false;
				}
			});
		}
	}

	private exit(resurgence: boolean){
		BasePopUp.jelly_leave(this.node)
		.then(()=>{
			this.close(resurgence);
			this.btn_lock = false;
		})
	}
}

